package com.power.sifa.correction.model;


import cn.hutool.core.collection.CollectionUtil;
import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.plf.core.common.annotations.FieldEnumText;
import com.plf.core.common.domain.BaseModel;
import com.power.sifa.correction.model.enumeration.Tp_crc_rule;
import com.power.sifa.correction.model.pojo.bo.CheckRuleResultBO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

/**
*  预警信息违规详情实体类
*/

@Entity
@ApiModel(value="预警信息违规详情")
@Data
@Table(name="Cre_warning_detail",schema="sfapprove")
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
//@IdClass(Cre_warning_detail.Cre_warning_detailPK.class) 用于复合组件
public class Cre_warning_detail extends BaseModel implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
    @Id
    @Setter(AccessLevel.NONE)
    @ApiModelProperty(value = "详情记录id")
    private String id;

    @ApiModelProperty(value = "预警信息id")
    private String warningid;

    @ApiModelProperty(value = "规则id")
    private String ruleid;

    @ApiModelProperty(value = "预警时间", example="2020-10-22 10:03:22")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date warningtime;

    @ApiModelProperty(value = "预警地点")
    private String addr;

    @ApiModelProperty(value = "校验结果")
    private Integer validate;

    @ApiModelProperty(value = "单条描述")
    private String describe;

    @ApiModelProperty(value = "违规类型")
    private Integer type;

    @ApiModelProperty(value = "规则名称")
    private String rulename;

    @ApiModelProperty(value = "生成时间", example="2020-10-22 10:03:22")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createtime;

    @ApiModelProperty(value = "公安id")
    private Integer gaid;

    /**
     *  违规类型文本
     */
    @Transient
    @FieldEnumText(eum="Tp_crc_rule", valuefield="type")
    private String type_text;


    public Map<String, Object> parseMap() {
        Map<String, Object> map=new HashMap<>();
        map.put("id", this.getId());
        map.put("warningid", this.getWarningid());
        map.put("ruleid", this.getRuleid());
        map.put("warningtime", this.getWarningtime());
        map.put("addr", this.getAddr());
        map.put("validate", this.getValidate());
        map.put("describe", this.getDescribe());
        map.put("type_text", this.type_text);
        map.put("type", this.getType());
        map.put("rulename", this.getRulename());
        map.put("createtime", this.getCreatetime());
        return map;
    }

    public Cre_warning_detail(Cre_warning cre_warning, CheckRuleResultBO checkRuleResult) {
        this.id = NanoIdUtils.randomNanoId();
        this.warningid = cre_warning.getId();
        this.ruleid = checkRuleResult.getRuleId();
        this.warningtime = cre_warning.getRectime();
        this.addr = cre_warning.getAddr();
        this.validate = checkRuleResult.getValidate() ? 1 : 0;
        this.describe = CollectionUtil.isNotEmpty(checkRuleResult.getDescribeList()) ?
                String.join("，", checkRuleResult.getDescribeList()) : "";
        if (StringUtils.isNotEmpty(checkRuleResult.getRuleId())) {
            String firstLetter = checkRuleResult.getRuleId().substring(0, 1).toUpperCase(Locale.ROOT);
            switch (firstLetter) {
                case "A":
                    this.type = Tp_crc_rule.ZONEOUT;
                    break;
                case "B":
                    this.type = Tp_crc_rule.ZONEIN;
                    break;
                case "C":
                    this.type = Tp_crc_rule.NIGHTOUT;
                    break;
            }
        }
        this.rulename = checkRuleResult.getRuleName();
        this.createtime = new Date();
        this.gaid = checkRuleResult.getGaid();
    }

    public static List<Cre_warning_detail> convertToList(Cre_warning cre_warning, List<CheckRuleResultBO> checkRuleResults) {
        List<Cre_warning_detail> cre_warning_details = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(checkRuleResults)) {
            checkRuleResults.stream().forEach(el -> {
                cre_warning_details.add(new Cre_warning_detail(cre_warning, el));
            });
        }
        return cre_warning_details;
    }

    public String getId() {
        return id;
    }

    public void setId(Serializable id) {
        this.id = (String) id;
    }

    public Serializable getPrimaryKey() {
        return id;
        // return new Cre_warning_detailPK(id, id2); 用于复合主键，请手动创建内部静态类
    }

    public void setPrimaryKey(Serializable key) {
        this.id = (String)key;

        //Cre_warning_detailPK pk = (Cre_warning_detailPK) key; 用于复合主键
        //this.id = pk.getId();
        //this.id2 = pk.getId2();
    }

}